package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.service.MenuPermissionInitService;
import com.example.service.MenuPermissionMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜单权限映射管理控制器
 */
@RestController
@RequestMapping("/api/menu-permission-mapping")
@CrossOrigin
public class MenuPermissionMappingController {

    @Autowired
    private MenuPermissionMappingService menuPermissionMappingService;

    @Autowired
    private MenuPermissionInitService menuPermissionInitService;

    /**
     * 根据菜单ID获取对应的权限ID列表
     */
    @PostMapping("/permissions-by-menus")
    @RequirePermission("admin:menu:permission:view")
    public Result<Set<Long>> getPermissionsByMenus(@RequestBody List<Long> menuIds) {
        try {
            Set<Long> permissionIds = menuPermissionMappingService.getPermissionsByMenus(menuIds);
            return Result.success(permissionIds);
        } catch (Exception e) {
            return Result.error("获取菜单对应权限失败：" + e.getMessage());
        }
    }

    /**
     * 根据菜单编码获取对应的权限编码列表
     */
    @PostMapping("/permission-codes-by-menu-codes")
    @RequirePermission("admin:menu:permission:view")
    public Result<Set<String>> getPermissionCodesByMenuCodes(@RequestBody List<String> menuCodes) {
        try {
            Set<String> permissionCodes = menuPermissionMappingService.getPermissionCodesByMenuCodes(menuCodes);
            return Result.success(permissionCodes);
        } catch (Exception e) {
            return Result.error("获取菜单对应权限编码失败：" + e.getMessage());
        }
    }

    /**
     * 根据菜单路径获取对应的权限编码列表
     */
    @GetMapping("/permission-codes-by-path")
    @RequirePermission("admin:menu:permission:view")
    public Result<Set<String>> getPermissionCodesByMenuPath(@RequestParam String menuPath) {
        try {
            Set<String> permissionCodes = menuPermissionMappingService.getPermissionCodesByMenuPath(menuPath);
            return Result.success(permissionCodes);
        } catch (Exception e) {
            return Result.error("获取菜单路径对应权限编码失败：" + e.getMessage());
        }
    }

    /**
     * 为角色分配菜单时，同时分配对应的权限
     */
    @PostMapping("/assign-permissions-for-menus")
    @RequirePermission("admin:role:menu:assign")
    public Result<String> assignPermissionsForMenus(@RequestParam Long roleId, @RequestBody List<Long> menuIds) {
        try {
            int assignedCount = menuPermissionMappingService.assignPermissionsForMenus(roleId, menuIds);
            return Result.success("为角色分配菜单对应权限成功，共分配 " + assignedCount + " 个权限");
        } catch (Exception e) {
            return Result.error("分配菜单对应权限失败：" + e.getMessage());
        }
    }

    /**
     * 初始化菜单权限映射关系
     */
    @PostMapping("/init")
    @RequirePermission("admin:system:init")
    public Result<String> initMenuPermissionMapping() {
        return menuPermissionInitService.initMenuPermissionMapping();
    }

    /**
     * 检查菜单权限映射完整性
     */
    @GetMapping("/check")
    @RequirePermission("admin:system:check")
    public Result<String> checkMenuPermissionMapping() {
        return menuPermissionInitService.checkMenuPermissionMapping();
    }

    /**
     * 修复菜单权限映射关系
     */
    @PostMapping("/fix")
    @RequirePermission("admin:system:fix")
    public Result<String> fixMenuPermissionMapping() {
        return menuPermissionInitService.fixMenuPermissionMapping();
    }
} 